<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/10
 * Time: 9:47
 */
namespace App\Http\Controllers\Admin;
use \App\Http\Controllers\Controller;
use App\Model\AdminUser;
use App\Model\Branch;
use App\Model\User;
//use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use Session;
use Auth;
use Log;
class LoginController extends Controller
{
    /**
     * login
     */
    public function getLogin()
    {
        return view('Admin/login');
    }

    /**
     * @param Request $request
     * @return $this|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\think\response\Redirect|void
     */
    public function login(Request $request)
    {
        $this->validate( $request,
            [
                'name'       => 'required',
                'password'       => 'required',

            ],
            [
                'name.required'       => '用户名必填',
                'password.required'        => '密码必填',

            ]
        );
        //获取验证码
        $captcha = $request->input('captcha');
        //检测验证码
        if (Session::get('milkcaptcha') == $captcha) {
            //获取表单数据
            $user = request(['name', 'password']);
            //登陆验证
            if (true == \Auth::guard('admin')->attempt($user)) {
                $data['user']['ip'] = $_SERVER["REMOTE_ADDR"];
                $data['user']['login_time'] = time();
                AdminUser::where('name',$user['name'])->update(array_only($data['user'],['ip','login_time']));
               return redirect('/admin/index');
            } else {
                return \Redirect::back()->withErrors("用户名密码错误");
            }
            return \Redirect::back()->withErrors("用户名密码错误");
        } else {
           // 用户输入验证码错误
            return redirect()->back()->withErrors('验证码输入错误！');
        }
    }

}